package com.wits.platform.common.utils;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * EhCache工具类
 */
public class CacheUtil implements ApplicationContextAware {

	public static final String COMET_CACHE = "cometCache";
	

	private static CacheManager customEhcacheManager;


	public static Object get(String cacheName, String key) {
		Element element = customEhcacheManager.getCache(cacheName).get(key);
		return element == null ? null : element.getObjectValue();
	}

	public static void put(String cacheName, String key, Object value) {
		Element element = new Element(key, value);
		customEhcacheManager.getCache(cacheName).put(element);
	}

	public static void remove(String cacheName, String key) {
		customEhcacheManager.getCache(cacheName).remove(key);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		customEhcacheManager = (CacheManager) applicationContext
				.getBean("qsoEhcacheManager");
	}

}
